﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;


namespace MsDAL
{
    public partial class Buy3a00DAL : Buy3a00Base
    {
        /// <summary>
        /// 生成新的采购单号
        /// </summary>
        /// <param name="perid">供应商内码</param>
        /// <param name="muser"></param>
        /// <returns></returns>
        public string GetDocno(string perid, string muser)
        {
            string sql = "sp_createdocno";

            SqlParameter[] parameters = {
					new SqlParameter("@providerid",SqlDbType.NVarChar),  
					new SqlParameter("@muser", SqlDbType.NVarChar),
                    new SqlParameter("@newsn", SqlDbType.NVarChar,100)
					};
            parameters[0].Value = perid;
            parameters[1].Value = muser;
            parameters[2].Value = "";
            parameters[0].Direction = ParameterDirection.Input;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[2].Direction = ParameterDirection.Output;

            DBHelper.ExecuteProc(sql, parameters);

            object o = parameters[2].Value;
            return null == o ? "" : o.ToString();
        }

        public SqlApp Arrival(string flag, Buy3a01 model, string arrno, DateTime arrdate)
        {
            string sql = string.Format("sp_arrival '{0}','{1}','{2}','{3}','{4}','{5}','{6}' ",
                model.Listid, model.Accqty, model.Accpreqty, model.Accamt, arrno, arrdate, flag);

            return new SqlApp(sql, null, false, "");
        }

        /// <summary>
        /// 审核
        /// </summary>
        public SqlApp Audit(string listid)
        {
            string sql = "UPDATE Buy3a00 SET audstatus = '2' WHERE listid=@listid AND audstatus ='1'";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 订单审核失败！");
        }

        public SqlApp UpdateAcceptComplete(string listid)
        {
            string sql = "UPDATE Buy3a00 SET ordstatus = '10' WHERE listid=@listid AND ordstatus <> '10'";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 订单的状态已经改变，收货完成操作失败！");
        }

        //删除
        public SqlApp DeleteDocno(string listid)
        {
            string sql = "UPDATE Buy3a00 SET ordstatus = '9' WHERE listid=@listid AND audstatus ='1'";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 订单的状态已经改变，删除失败！");
        }

        public SqlApp DoAdjust(string id, double adjQty, double adjAmt)
        {
            var sql = "update buy3a00 set adjtotalqty = isnull(adjtotalqty,0) + @adjtotalqty, adjtotalamt=isnull(adjtotalamt, 0) + @adjtotalamt where listid =@listid";

            SqlParameter[] parameters = {
				new SqlParameter("@adjtotalqty", SqlDbType.Decimal),
				new SqlParameter("@adjtotalamt", SqlDbType.Decimal),
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = adjQty;
            parameters[1].Value = adjAmt;
            parameters[2].Value = id;

            return new SqlApp(sql, new ArrayList(parameters), false, "数据保存失败！");
        }

    }
}
